﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace super_scrable.Models
{
    public class Word : IComparable
    {
        protected List<Letter> letters;
        protected static List<Word> words;
        protected int value;

        public int Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public Word()
        {
            letters = new List<Letter>();
            value = 1;
        }

        public Word(List<Letter> letters) : this()
        {
            this.letters = letters;
        }

        public Word(List<Letter> letters, int value) : this()
        {
            this.letters = letters;
            this.value = value;
        }

        public static void InitializeStatic()
        {
            words = LoadWords();
        }

        public int CountPoints()
        {
            int points = 0;

            foreach (Letter letter in letters)
                points += letter.Point;

            return points;
        }

        public Boolean Validate()
        {


            return true;
        }

        public void AddLetter(Letter letter)
        {
            letters.Add(letter);
        }

        public static List<Word> LoadWords()
        {
            List<Word> words = new List<Word>();
            StringCollection actives_language = super_scrable.Properties.Settings.Default.active_languages;

            foreach (string language in actives_language)
            {
                try
                {
                    string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), super_scrable.Properties.Settings.Default.language_dir, language + ".txt");
                    StreamReader file = new StreamReader(path);
                    String line;

                    while ((line = file.ReadLine()) != null)
                    {
                        Word word = new Word();
                        foreach (Char c in line)
                        {
                            word.letters.Add(new Letter(c));
                        }
                        words.Add(word);
                    }
                }
                catch (Exception e) { }
            }

            return words;
        }

        public int CompareTo(Object o)
        {
            if (o == null) return 1;

            Word word = o as Word;

            for (int i = 0; i < word.letters.Count && i < this.letters.Count; i++)
            {
                if(this.letters[i].CompareTo(word.letters[i]) != 0) return this.letters[i].CompareTo(word.letters[i]);
            }
            return 0;
        }

        public static Boolean SearchWord(Word word)
        {
            foreach(Word w in words)
                if(w.CompareTo(word) == 0)
                    return true;

            return false;
        }

        override public String ToString()
        {
            String s = "";

            foreach (Letter l in letters)
                s += l.ToString() + " ";

            return s;
        }
    }
}